﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyDoanVien.General;
using System.Data;
using System.Data.SqlClient;
using QuanLyDoanVien.Bussiness;

namespace QuanLyDoanVien.DataLayer
{
    class ChucVuData
    {
        DataService m_ChucVuData = new DataService();

        public List<ChucVuInfo> Lay_DS_ChucVu()
        {
            List<ChucVuInfo> cv = new List<ChucVuInfo>();
            DataService.OpenConnection();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Tab_ChucVu");
            m_ChucVuData.Load(cmd);
            foreach (DataRow dr in m_ChucVuData.Rows)
            {
                ChucVuInfo kl = new ChucVuInfo();
                kl.MaChucVu = dr["MaChucVu"].ToString();
                kl.TenChucVu = dr["TenChucVu"].ToString();
                cv.Add(kl);
            }
            m_ChucVuData.CloseConnection();
            return cv;
        }
        public DataTable LayChucVu()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM Tab_ChucVu");
            m_ChucVuData.Load(cmd);
            return m_ChucVuData;
        }
        public void ThemChucVu(DataRow row)
        {
            m_ChucVuData.Rows.Add(row);
        }

        #region Them Chuc Vu
        public bool LuuChucVu()
        {
            return m_ChucVuData.ExecuteNoneQuery() > 0;
        }

        public void LuuChucVu(ChucVuInfo cv)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO Tab_ChucVu VALUES(@MaChucVu, @TenChucVu)");
            cmd.Parameters.Add("MaChucVu", SqlDbType.Char).Value = cv.MaChucVu;
            cmd.Parameters.Add("TenChucVu", SqlDbType.NVarChar).Value = cv.TenChucVu;
            m_ChucVuData.Load(cmd);
        }
        #endregion

        #region Sua Chuc Vu
        public bool SuaChucVu(ChucVuInfo cv)
        {
            SqlCommand cmd = new SqlCommand("UPDATE Tab_ChucVu SET TenChucVu = @TenChucVu WHERE MaChucVu = @MaChucVu");
            cmd.Parameters.Add("MaChucVu", SqlDbType.Char).Value = cv.MaChucVu;
            cmd.Parameters.Add("TenChucVu", SqlDbType.NVarChar).Value = cv.TenChucVu;
            m_ChucVuData.Load(cmd);
            try
            {
                m_ChucVuData.ExecuteNoneQuery(cmd);
                m_ChucVuData.CloseConnection();
                return true;
            }
            catch
            {
                m_ChucVuData.CloseConnection();
                return false;
            }
        }
        #endregion

        #region Xoa Chuc Vu
        public bool XoaChucVu(String machucvu)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM Tab_ChucVu WHERE MaChucVu = @MaChucVu");
            cmd.Parameters.Add("MaChucVu", SqlDbType.Char).Value = machucvu;
            m_ChucVuData.Load(cmd);
            try
            {
                m_ChucVuData.ExecuteNoneQuery(cmd);
                m_ChucVuData.CloseConnection();
                return true;
            }
            catch
            {
                m_ChucVuData.CloseConnection();
                return false;
            }
        }

        #endregion

        public string getAutoID()
        {
            DataService.OpenConnection();
            QuyDinh qd = new QuyDinh();
            string id = "";
            string sql = "select top 1 MaChucVu from Tab_ChucVu Order By MaChucVu desc";
            SqlCommand command = new SqlCommand(sql);
            m_ChucVuData.Load(command);
            if (m_ChucVuData.Rows.Count < 1)
                return "CV00000001";
            id = m_ChucVuData.Rows[0][0].ToString();
            int num = int.Parse(id.Substring(2, 8)) + 1;
            id = "CV" + qd.LaySTT(num);
            m_ChucVuData.CloseConnection();
            return id;
        }

    }
}
